﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Raise3000.Code.Attribute_Calculator;

namespace Raise3000.Code.HandHistoryLoader
{
    public class HandHistoryManager
    {
        private CalculatorManager calculatorManager_ = null;

        private IHandHistoryLoader handHistoryLoader_ = null;

        // attributes
        private string site_ = null;
        private string stake_ = null;
        private int nrOfSeat_ = 0;
        private List<string> handHistoryFiles_ = null;

        public HandHistoryManager(string site, string stake, int nrOfSeat, List<string> handHistoryFiles)
        {
            site_ = site;
            stake_ = stake;
            nrOfSeat_ = nrOfSeat;
            handHistoryFiles_ = handHistoryFiles;
        }

        public void LoadHandHistory()
        {
            InitializeCalculatorManager();
            InitializeHandHistoryLoader();

            handHistoryLoader_.LoadHandHistories();
        }

        public List<PlayerTemplate> CreatePlayerTemplatesFromHistory()
        {
            LoadHandHistory();

            List<PlayerTemplate> playerTemplates = new List<PlayerTemplate>();

            List<string> playersList = calculatorManager_.GetAllPlayersRegisteredToCalculators();
            foreach (string player in playersList)
            {
                string playerTemplateName = AddPostFixToPlayerName(player);

                PlayerTemplate playerTemplate = new PlayerTemplate(playerTemplateName);

                List<ICalculator> calculators = calculatorManager_.GetCalculators();

                foreach (ICalculator calculator in calculators)
                {
                    StatisticalAttribute sa = calculator.CreateStatisticalAttribute(player);
                    playerTemplate.AddAttribute(sa);
                }

                playerTemplates.Add(playerTemplate);
            }

            return playerTemplates;
        }

        private void InitializeCalculatorManager()
        {
            calculatorManager_ = new CalculatorManager();

            calculatorManager_.RegisterCalculator(new Attr_VPIPCalculator());
            calculatorManager_.RegisterCalculator(new Attr_BetCalculator());
            calculatorManager_.RegisterCalculator(new Attr_ThreeBetCalculator());
        }

        private void InitializeHandHistoryLoader()
        {
            if (site_ == "AbsolutePoker")
            {
                handHistoryLoader_ = new AbsolutePokerHandHistoryLoader(handHistoryFiles_, calculatorManager_, nrOfSeat_);
            }
        }

        private string AddPostFixToPlayerName(string playerName)
        {
            string postFix = "_" + nrOfSeat_.ToString() + "seat_" + stake_;
            return playerName + postFix;
        }
    }
}
